package hvzt.minimap.activities;

import hvzt.minimap.R;

import hvzt.minimap.ambassador.Ambassador;
import hvzt.minimap.asynctasks.Progress;
import iface.Kill;
import iface.Status;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ProgressBar;

/**
 * KillActivitity the UI responsible for users for inputting kill data 
 * to kill users, and to return user response messages. 
 * @author JosephM
 */
public class KillActivity extends Activity implements OnClickListener,ServerActivity {
	
	private EditText netId;
	private EditText humanId;
	private String registration;
	ProgressBar spinner;
	private AlertDialog alert;
	
	// override from extending activity, setup xml layouts and UI.
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.kill);
		setUpUI();
	}

	// private helper to setup UI
	private void setUpUI() {
		netId = (EditText)findViewById(R.id.net_id_field);
		humanId = (EditText)findViewById(R.id.human_id_field);
		// not allow user to use different ID
		netId.setEnabled(false);
		SharedPreferences prefs = getSharedPreferences("hvzt", MODE_PRIVATE);
		// automatically fill in the userId that from registration. 
		netId.setText(prefs.getString("netId", ""));
		registration = null;
		spinner = (ProgressBar)findViewById(R.id.spinner);
		spinner.setVisibility(View.INVISIBLE);
	}
	
	// method for when user clicks on kill button
	@Override
	public void onClick(View arg0) {
		String netIdField = netId.getText().toString();
		String humanIdField = (humanId.getText().toString());
		// empty/null netId field
		if (netId.length() == 0 || netId == null) {
			showDialog(0);
			// empty/null humanId field
		} else if (humanId.length() == 0 || humanId == null) {
			showDialog(1);
		} else {
			Kill kill = new Kill(netIdField, humanIdField);
			Ambassador.getInstance().sendKill(kill, this);	
		}
	}
	
	// method for showing progress depending on progress returned back from killtask
	@Override
	public void updateProgress(Progress progress) {
		Log.e("test", progress.toString());
		if (progress == Progress.IN_PROGRESS) {
			spinner.setVisibility(View.VISIBLE);
		} else {
			spinner.setVisibility(View.INVISIBLE);
		}
	}

	// method for showing results depending on progress returned back from killtask
	@Override
	public void handleResult(Status result) {
		Log.e("test",result.toString());
		registration = result.toString();
		if (result.getMessageStatus() == Status.Result.ERROR || result.getMessageStatus() == Status.Result.INVALID) {
			showDialog(2);
		} else {
			showDialog(3);
		}
	}
	
	// the different dialogs to popup when user submits a kill.
	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		switch(id) {
		case 0:
			builder.setMessage("Invalid netId format").setCancelable(true);
			break;
		case 1:
			builder.setMessage("Invalid humanId format").setCancelable(true);
			break;
		case 2:	
			builder.setMessage(registration).setCancelable(true);
			break;
		case 3:
			builder.setMessage("Kill Successful! Going back to map").setCancelable(false)
			.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			});
			break;
		}
		alert = builder.create();
		return alert;
	}
	
	// in order for error message to properly update each time its required. 
	@Override
	protected void onPrepareDialog(int id, Dialog dialog) {
		switch(id) {
		case 2:	
			AlertDialog alertDialog = (AlertDialog) dialog;
            alertDialog.setMessage(registration);
            break;
		}
		super.onPrepareDialog(id, dialog);
	}
	
	@Override
	protected void onDestroy() {
		if (alert != null)
			alert.cancel();
		super.onDestroy();
	}
}
